package com.abc.recycling.util;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AddressUtils {
    
    private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true";
    
    public static String getRealAddressByIP(String ip) {
        String address = "未知";
        try {
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> response = restTemplate.getForEntity(String.format(IP_URL, ip), String.class);
            String body = response.getBody();
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(body);
            String region = node.get("pro").asText();
            String city = node.get("city").asText();
            address = region + " " + city;
        } catch (Exception e) {
            // 出现异常则返回未知地址
        }
        return address;
    }
} 